import Vue from 'vue'
import Router from 'vue-router'
import state from '../store/state';

// 年报
const YearReportPlatform = () => import('@/pages/yearReport/platform2020/index')
const YearReportUnit = () => import('@/pages/yearReport/unit2020/Index')

// 月报/季报
const UnitReport = () => import('@/pages/reportUnit/Index')
const PlatformReport = () => import('@/pages/reportPlatform/Index')

// 巡检
const InspectMain = () => import('@/pages/inspect/InspectMain')
// 巡检任务详情
const InspectDetail = () => import('@/pages/inspect/InspectDetail')
// 巡检点位详情
const InspectPoint = () => import('@/pages/inspect/InspectPoint')
// 巡检-全部点位
const InspectAllPoint = () => import('@/pages/inspect/InspectAllPoint')
// 新增巡检点位
const InspectAllPointEdit = () => import('@/pages/inspect/InspectAllPointEdit')
// 全部巡检点位详情
const InspectAllPointDetail = () => import('@/pages/inspect/InspectAllPointDetail')
// 巡检统计
const InspectStatistic = () => import('@/pages/inspect/InspectStatistic')
// 巡检单位排名
const InspectUnitRank = () => import('@/pages/inspect/UnitRank')
// 巡检个人排名
const InspectPersonalRank = () => import('@/pages/inspect/PersonalRank')
const Subscribe = () => import('@/pages/subscribe/Index')
const ChooseFbs = () => import('@/pages/subscribe/ChooseFbs')
const AddDevice = () => import('@/pages/device/AddDevice')
const AddRecord = () => import('@/pages/device/AddRecord')
const DeviceForm = () => import('@/pages/device/DeviceForm')
const DeviceFormOther = () => import('@/pages/device/DeviceFormOther')
const Verifying = () => import('@/pages/device/Verifying')
const VerifySuccess = () => import('@/pages/device/VerifySuccess')
const VerifyFail = () => import('@/pages/device/VerifyFail')
const DtuData = () => import('@/pages/device/DtuData')
const VideoPlayer = () => import('@/pages/video/VideoPlayer')

Vue.use(Router)

const router = new Router({
  // 解决路由跳转页面没有置顶
  scrollBehavior(to, from, savedPosition) {
    if (to.name == 'InspectDetail') return {}
    if (savedPosition) {
      return savedPosition
    } else {
      return {
        x: 0,
        y: 0
      }
    }
  },
  routes: [{
    path: '/VideoPlayer',
    name: 'VideoPlayer',
    component: VideoPlayer,
    meta: {
      title: '视频播放',
    }
  }, {
    path: '/AddDevice',
    name: 'AddDevice',
    component: AddDevice,
    meta: {
      title: '添加设备',
    }
  }, {
    path: '/AddRecord',
    name: 'AddRecord',
    component: AddRecord,
    meta: {
      title: '添加记录',
      keepAlive: true
    }
  }, {
    path: '/DeviceForm',
    name: 'DeviceForm',
    component: DeviceForm,
    meta: {
      title: '添加设备'
    }
  }, {
    path: '/DeviceFormOther',
    name: 'DeviceFormOther',
    component: DeviceFormOther,
    meta: {
      title: '添加设备'
    }
  }, {
    path: '/Verifying',
    name: 'Verifying',
    component: Verifying,
    meta: {
      title: '验证'
    }
  }, {
    path: '/VerifySuccess',
    name: 'VerifySuccess',
    component: VerifySuccess,
    meta: {
      title: '验证结果'
    }
  }, {
    path: '/VerifyFail',
    name: 'VerifyFail',
    component: VerifyFail,
    meta: {
      title: '验证结果'
    }
  }, {
    path: '/DtuData',
    name: 'DtuData',
    component: DtuData,
    meta: {
      title: 'DTU上传数据'
    }
  }, {
    path: '/ChooseFbs',
    name: 'ChooseFbs',
    component: ChooseFbs,
    meta: {
      title: '选择建筑物'
    }
  }, {
    path: '/Subscribe',
    name: 'Subscribe',
    component: Subscribe,
    meta: {
      title: '警情订阅',
      keepAlive: true
    }
  }, {
    path: '/InspectMain',
    name: 'InspectMain',
    component: InspectMain,
    meta: {
      title: '日常巡检',
      keepAlive: true
    }
  }, {
    path: '/InspectDetail',
    name: 'InspectDetail',
    component: InspectDetail,
    meta: {
      title: '巡检任务详情'
    }
  }, {
    path: '/InspectPoint',
    name: 'InspectPoint',
    component: InspectPoint,
    meta: {
      title: '巡检点位内容'
    }
  }, {
    path: '/InspectAllPoint',
    name: 'InspectAllPoint',
    component: InspectAllPoint,
    meta: {
      title: '巡检点位',
      keepAlive: true
    }
  }, {
    path: '/InspectAllPointEdit',
    name: 'InspectAllPointEdit',
    component: InspectAllPointEdit,
    meta: {
      title: '巡检点位'
    }
  }, {
    path: '/InspectAllPointDetail',
    name: 'InspectAllPointDetail',
    component: InspectAllPointDetail,
    meta: {
      title: '巡检点位详情'
    }
  }, {
    path: '/InspectStatistic',
    name: 'InspectStatistic',
    component: InspectStatistic,
    meta: {
      title: '巡检统计',
      keepAlive: true
    }
  }, {
    path: '/InspectUnitRank',
    name: 'InspectUnitRank',
    component: InspectUnitRank,
    meta: {
      title: '巡检单位排名'
    }
  }, {
    path: '/InspectPersonalRank',
    name: 'InspectPersonalRank',
    component: InspectPersonalRank,
    meta: {
      title: '巡检个人排名',
      keepAlive: true
    }
  }, {
    path: '/YearReportPlatform',
    name: 'YearReportPlatform',
    component: YearReportPlatform,
    meta: {
      title: '年度平台安全年报'
    }
  }, {
    path: '/YearReportUnit',
    name: 'YearReportUnit',
    component: YearReportUnit,
    meta: {
      title: '年度消防年报'
    }
  }, {
    path: '/UnitReport',
    name: 'UnitReport',
    component: UnitReport,
    meta: {
      title: '消防报表'
    }
  }, {
    path: '/PlatformReport',
    name: 'PlatformReport',
    component: PlatformReport,
    meta: {
      title: '消防报表'
    }
  }]
})

//路由守卫
router.beforeEach((to, from, next) => {
  if (to.name != 'InspectDetail') {
    window.scrollTo(0, 0)
  }
  document.title = to.meta.title || '智慧易消防'
  if (to.meta.requireAuth) {
    if (state.userInfo.accountName) {
      next()
    } else {
      next({
        path: '/login'
      })
    }
  } else {
    next()
  }
})

export default router
